<?php

namespace app\admin\controller\clue;

use app\common\controller\Backend;
use app\common\library\Auth;
use think\Db;

/**
 * 课程管理
 *
 * @icon fa fa-user
 */
class Clue extends Backend
{

    protected $relationSearch = true;
    protected $searchFields = 'id,username,nickname';
    protected $noNeedRight = ['select_school_class','select_teacher','select_class_room','getClassRoom','getTeacher','getSchoolClass'];

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Thread');
    }

    /**
     * 查看
     */
    public function index()
    {
        $this->relationSearch =true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $count = $this->model
                ->with(['saleman'])
                ->where($where)
                ->count();
            $list = $this->model
                ->with(['saleman'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset,$limit)
                ->select();
            foreach ($list as $w=>&$v){
//                if($v['logo']){
//                    $v['logo'] = 'https://yunwotuojiao.oss-cn-fuzhou.aliyuncs.com'.$v['logo'];
//                }
            }

            $sql = $this->model->getLastSql();
            $result = array("total" => $count, "rows" => $list,'sql'=>$sql);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            $params['create_time'] = time();
            $params['admin_id'] = $this->auth->id;
            $image_arr = explode('aliyuncs.com',$params['logo']);
            if($params['logo']){
                $params['logo'] = $image_arr[1];
            }
            $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
            $randStr = str_shuffle($str);
            $rands = substr($randStr, 0, 6);
            $params['code'] = $rands;
            $res = $this->model->insert($params);
            if ($res){
                $this->success();
            }
            $this->error();
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids=''){
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                try {
                    $params['update_time'] = time();
                    $params['admin_id'] = $this->auth->id;
                    $image_arr = explode('aliyuncs.com',$params['logo']);
                    if($params['logo']){
                        $params['logo'] = $image_arr[1];
                    }
                    $result = $row->allowField(true)->save($params);
                    if ($result !== false) {
                        $this->success();
                    } else {
                        $this->error($row->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                } catch (\think\Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        if($row['logo']){
            $row['logo'] = 'https://yunwotuojiao.oss-cn-fuzhou.aliyuncs.com'.$row['logo'];
        }

        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        $row = $this->model->get($ids);
        $this->modelValidate = true;
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $this->model->where(['id'=>$row['id']])->delete();
        $this->success();
    }

    /**
     * 详情
     */
    public function detail($ids=''){
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                try {
                    $params['up_time'] = time();
                    $result = $row->allowField(true)->save($params);
                    if ($result !== false) {
                        $this->success();
                    } else {
                        $this->error($row->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                } catch (\think\Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }


    /**
     * 详情
     */
    public function do_assign($ids=''){
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                try {
                    $params['up_time'] = time();
                    $result = $row->allowField(true)->save($params);
                    if ($result !== false) {
                        $this->success();
                    } else {
                        $this->error($row->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                } catch (\think\Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
//        //获取详情信息
//        $this->model->getDetailArr($row);
        $this->view->assign("row", $row);
        if ($row['personnel_id'] > 0) {
            $row['is_have_saleman'] = 1;
        } else {
            $row['is_have_saleman'] = 0;
        }
        $row['salesman'] = Db::name('user')->where(['uid'=>$row['personnel_id']])->value('real_name');
        $this->assignconfig("is_have_saleman", $row['is_have_saleman']);
        return $this->view->fetch();
    }


    /**
     * 详情
     */
    public function do_most_assign($ids=''){
//        $row = $this->model->get($ids);
//        if (!$row) {
//            $this->error(__('No Results were found'));
//        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            $check_id = $this->request->get("check_id");
            if ($params) {
                try {
                    $check_id_arr = explode(',',$check_id);
                    if(count($check_id)==0){
                        $this->error('尚未选择任何一笔订单');
                    }
                    $params['up_time'] = time();
                    // $params['salesman_id'] = time();
                    $result =  $this->model->where(['id'=>['in',$check_id_arr],'salesman_id'=>0])->update($params);
                    if ($result !== false) {
                        $this->success();
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                } catch (\think\Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }

}
